#include "headerconf.h"
#include "uart_sew.h"
#include "spi_sew.h"
#include "adc_sew.h"
//#include "can_sew.h"



#define BufferSize 32
volatile TestStatus TransferStatus = FAILED;


//Private Funktions
void Delay(__IO uint32_t nCount);
TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength);



int main(void)
{
	int ADCvalue = 0;
	int16_t v25=1715;
	int16_t slope =35;
	int temperature = 0;
	//CanTxMsg Message;
	
	//Temperatursensor = new ADC(ADC_Channel_TempSensor,ADC_SampleTime_239Cycles5);

	ADC Temperatursensor(ADC_Channel_TempSensor,ADC_SampleTime_239Cycles5);	

	//create UART 1 on PINS 9 + 10 for serial communication
	ConnectionUSART Port1(1, 9600);
	
	
	//Init CAN	 
	//NVIC_Config();	 
	//CAN_Config();
  //CAN_ITConfig(CANx, CAN_IT_FMP0, ENABLE);

	


   	while(1)
  	{
			
			//read data from temperature sensor on ADC16
			ADCvalue = Temperatursensor.readValue();

			//send raw data got from ADC16	over USART
			Port1.SendData("ADC-Raw Wert: ");
			Port1.SendData((int)ADCvalue);
			 
			//convert measured adc value to grad celius	
			temperature=(int)((((v25-ADCvalue)*330000/40960)/slope)+25);

			//send failure if temperature <0
			if(temperature<0)
			{
				Port1.SendData("Wert ist negativ!\n");
			}
			Delay(50);

			//send temperature over USART
			Port1.SendData("Messwert in Grad Celius: ");
			Port1.SendData((int)temperature);
			
			Delay(5000000);
			

  	}
}



//void Delay10ms(unsigned long tick)
//{
// 	unsigned long timetick;
//
//    timetick = TimeTick;
//    while ((TimeTick - timetick) < tick);
//}
void Delay(__IO uint32_t nCount)
{
  for(; nCount != 0; nCount--);
}


#ifdef  USE_FULL_ASSERT

void assert_failed(uint8_t* file, uint32_t line)
{ 
  /*Error handling*/

  /* Infinite loop */
  while (1)
  {
  }
}
#endif
